package com.ymx.darling.dialect;

import com.ymx.darling.bean.sql.BoundSql;
import com.ymx.darling.bean.sql.SqlStatement;

import java.util.List;

/**
 * sqlite方言
 * @author 爱Java的小于
 */
public class SQLiteDialect implements Dialect {

    private String splitSql(String sql) {
        // SQLite 分页语法(与MySQL类似)
        return sql + " LIMIT ? OFFSET ?";
    }

    @Override
    public String getDataBaseType() {
        return "sqlite";
    }

    @Override
    public void paginate(BoundSql boundSql, int pageNum, int pageSize) {
        List<Object> paramList = boundSql.getRightParameters();
        int offset = (pageNum - 1) * pageSize;
        paramList.add(pageSize);    // LIMIT 值
        paramList.add(offset);       // OFFSET 值
        boundSql.setSql(this.splitSql(boundSql.getSql()));
    }

    @Override
    public void limit(SqlStatement statement, long limit, long offset) {
        List<Object> paramList = statement.getParamList();
        paramList.add(limit);       // LIMIT 值
        paramList.add(offset);      // OFFSET 值
        statement.setSql(this.splitSql(statement.getSql()));
    }

    @Override
    public String currentDate() {
        return "DATE('now')";
    }

    @Override
    public String currentTime() {
        return "TIME('now')";
    }

    @Override
    public String currentTimestamp() {
        return "DATETIME('now')";
    }
}
